!{sys.executable} --version
Python 3.7.9
import pandas as pd
import numpy as np
import plotly.express as px
import plotly
import plotly.graph_objs as go
from plotly import tools
from plotly.offline import init_notebook_mode, plot, iplot
data = pd.read_csv('https://raw.githubusercontent.com/datasets/covid-19/master/data/countries-aggregated.csv')
data.head()
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 0 | 2020-01-22 | Afghanistan | 0 | 0 | 0 |
| 1 | 2020-01-23 | Afghanistan | 0 | 0 | 0 |
| 2 | 2020-01-24 | Afghanistan | 0 | 0 | 0 |
| 3 | 2020-01-25 | Afghanistan | 0 | 0 | 0 |
| 4 | 2020-01-26 | Afghanistan | 0 | 0 | 0 |
data.tail(10)
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 82358 | 2021-03-16 | Zimbabwe | 36535 | 34124 | 1507 |
| 82359 | 2021-03-17 | Zimbabwe | 36552 | 34136 | 1508 |
| 82360 | 2021-03-18 | Zimbabwe | 36611 | 34226 | 1509 |
| 82361 | 2021-03-19 | Zimbabwe | 36652 | 34249 | 1510 |
| 82362 | 2021-03-20 | Zimbabwe | 36662 | 34257 | 1510 |
| 82363 | 2021-03-21 | Zimbabwe | 36665 | 34269 | 1512 |
| 82364 | 2021-03-22 | Zimbabwe | 36684 | 34315 | 1514 |
| 82365 | 2021-03-23 | Zimbabwe | 36717 | 34447 | 1516 |
| 82366 | 2021-03-24 | Zimbabwe | 36749 | 34476 | 1516 |
| 82367 | 2021-03-25 | Zimbabwe | 36778 | 34555 | 1518 |
data.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 82368 entries, 0 to 82367 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Date 82368 non-null object 1 Country 82368 non-null object 2 Confirmed 82368 non-null int64 3 Recovered 82368 non-null int64 4 Deaths 82368 non-null int64 dtypes: int64(3), object(2) memory usage: 3.1+ MB
data.describe()
| Confirmed | Recovered | Deaths | |
|---|---|---|---|
| count | 8.236800e+04 | 8.236800e+04 | 82368.000000 |
| mean | 2.015106e+05 | 1.184990e+05 | 5096.977455 |
| std | 1.194945e+06 | 6.261371e+05 | 24804.586512 |
| min | 0.000000e+00 | 0.000000e+00 | 0.000000 |
| 25% | 1.320000e+02 | 2.600000e+01 | 1.000000 |
| 50% | 4.236500e+03 | 1.976000e+03 | 74.000000 |
| 75% | 5.690275e+04 | 3.000775e+04 | 976.250000 |
| max | 3.007928e+07 | 1.126464e+07 | 546822.000000 |
fig = px.choropleth(data,locations='Country',locationmode='country names',color='Confirmed',animation_frame='Date')
fig.update_layout(title='Choropleth Map of Confirmed Cases -till today',template="plotly_dark")
fig.show()
fig = px.choropleth(data,locations='Country',locationmode='country names',color='Confirmed',animation_frame='Date',scope='africa')
fig.update_layout(title='Choropleth Map of Confirmed Cases - Africa till today',template="plotly_dark")
fig.show()
fig = px.scatter_geo(data,locations='Country',locationmode='country names',color='Confirmed',size='Confirmed',hover_name="Country",animation_frame='Date',title='Spread over Time')
fig.update(layout_coloraxis_showscale=True,layout_template="plotly_dark")
fig.show()
fig = px.choropleth(data,locations='Country',locationmode='country names',color='Recovered',animation_frame='Date')
fig.update_layout(title='Choropleth Map of Recovered Cases -till today',template="plotly_dark")
fig.show()
fig = px.scatter_geo(data,locations='Country',locationmode='country names',color='Recovered',size='Recovered',hover_name="Country",animation_frame='Date',title='Recovery over Time')
fig.update(layout_coloraxis_showscale=True,layout_template="plotly_dark")
fig.show()
fig = px.choropleth(data,locations='Country',locationmode='country names',color='Deaths',animation_frame='Date')
fig.update_layout(title='Choropleth Map of Deaths -till today',template="plotly_dark")
fig.show()
fig = px.scatter_geo(data,locations='Country',locationmode='country names',color='Deaths',size='Deaths',hover_name="Country",animation_frame='Date',title='Deaths over Time')
fig.update(layout_coloraxis_showscale=True,layout_template="plotly_dark")
fig.show()
import geopy
from geopy.geocoders import Nominatim
geolocator=Nominatim(user_agent="app")
location = geolocator.geocode("Egypt")
print(location.latitude, location.longitude)
26.2540493 29.2675469
# copy the data to make changing
df = data.copy()
df
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 0 | 2020-01-22 | Afghanistan | 0 | 0 | 0 |
| 1 | 2020-01-23 | Afghanistan | 0 | 0 | 0 |
| 2 | 2020-01-24 | Afghanistan | 0 | 0 | 0 |
| 3 | 2020-01-25 | Afghanistan | 0 | 0 | 0 |
| 4 | 2020-01-26 | Afghanistan | 0 | 0 | 0 |
| ... | ... | ... | ... | ... | ... |
| 82363 | 2021-03-21 | Zimbabwe | 36665 | 34269 | 1512 |
| 82364 | 2021-03-22 | Zimbabwe | 36684 | 34315 | 1514 |
| 82365 | 2021-03-23 | Zimbabwe | 36717 | 34447 | 1516 |
| 82366 | 2021-03-24 | Zimbabwe | 36749 | 34476 | 1516 |
| 82367 | 2021-03-25 | Zimbabwe | 36778 | 34555 | 1518 |
82368 rows × 5 columns
df[df['Country']=='Egypt'].tail(10)
| Date | Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|---|
| 23156 | 2021-03-16 | Egypt | 192195 | 148089 | 11384 |
| 23157 | 2021-03-17 | Egypt | 192840 | 148424 | 11431 |
| 23158 | 2021-03-18 | Egypt | 193482 | 148823 | 11472 |
| 23159 | 2021-03-19 | Egypt | 194127 | 149256 | 11512 |
| 23160 | 2021-03-20 | Egypt | 194771 | 149489 | 11557 |
| 23161 | 2021-03-21 | Egypt | 195418 | 149934 | 11598 |
| 23162 | 2021-03-22 | Egypt | 196061 | 150424 | 11637 |
| 23163 | 2021-03-23 | Egypt | 196709 | 150924 | 11680 |
| 23164 | 2021-03-24 | Egypt | 197350 | 151444 | 11720 |
| 23165 | 2021-03-25 | Egypt | 198011 | 151765 | 11768 |
# Know the maximum value
df2=df.groupby(['Country'])[['Confirmed','Recovered','Deaths']].max().reset_index()
df2
| Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|
| 0 | Afghanistan | 56226 | 49937 | 2467 |
| 1 | Albania | 122767 | 87760 | 2184 |
| 2 | Algeria | 116543 | 81065 | 3071 |
| 3 | Andorra | 11687 | 11111 | 114 |
| 4 | Angola | 21914 | 20190 | 532 |
| ... | ... | ... | ... | ... |
| 187 | Vietnam | 2579 | 2265 | 35 |
| 188 | West Bank and Gaza | 232038 | 205289 | 2521 |
| 189 | Yemen | 3816 | 1580 | 810 |
| 190 | Zambia | 87318 | 83895 | 1191 |
| 191 | Zimbabwe | 36778 | 34555 | 1518 |
192 rows × 4 columns
df2[df2['Country']=='Egypt']
| Country | Confirmed | Recovered | Deaths | |
|---|---|---|---|---|
| 53 | Egypt | 198011 | 151765 | 11768 |
lat_lon=[]
geolocator=Nominatim(user_agent="app")
for location in df2['Country']:
location = geolocator.geocode(location)
if location is None:
lat_lon.append(np.nan)
else:
geo=(location.latitude,location.longitude)
lat_lon.append(geo)
lat_lon
[(33.7680065, 66.2385139), (41.000028, 19.9999619), (28.0000272, 2.9999825), (42.5407167, 1.5732033), (-11.8775768, 17.5691241), (17.2234721, -61.9554608), (-34.9964963, -64.9672817), (40.7696272, 44.6736646), (-24.7761086, 134.755), (47.69641095, 14.754807384371395), (40.3936294, 47.7872508), (24.7736546, -78.0000547), (26.1551249, 50.5344606), (24.4768783, 90.2932426), (13.1500331, -59.5250305), (53.4250605, 27.6971358), (50.6402809, 4.6667145), (16.8259793, -88.7600927), (9.2231105, 2.31006719638123), (27.549511, 90.5119273), (-17.0568696, -64.9912286), (44.3053476, 17.5961467), (-23.1681782, 24.5928742), (-10.3333333, -53.2), (4.4137155, 114.5653908), (42.72572155, 25.336199594318522), (12.0753083, -1.6880314), (17.1750495, 95.9999652), (-3.3634357, 29.8870575), (16.0000552, -24.0083947), (13.5066394, 104.869423), (4.6125522, 13.1535811), (61.0666922, -107.991707), (7.0323598, 19.9981227), (15.6134137, 19.0156172), (-31.7613365, -71.3187697), (35.000074, 104.999927), (2.8894434, -73.783892), (-12.2045176, 44.2832964), (-0.7179165000000001, 16.018048574081842), (-2.9814344, 23.8222636), (9.5773243, -83.82371114343024), (7.9897371, -5.5679458), (45.5643442, 17.0118954), (23.0131338, -80.8328748), (34.9823018, 33.1451285), (49.8167003, 15.4749544), (55.670249, 10.3333283), (53.8953584, 27.5554078), (11.85677545, 42.757784519943655), (19.28131815, -70.035906834967), (19.28131815, -70.035906834967), (-1.3397668, -79.3666965), (26.2540493, 29.2675469), (13.8000382, -88.9140683), (1.613172, 10.5170357), (15.9500319, 37.9999668), (58.7523778, 25.3319078), (-26.5624806, 31.3991317), (10.2116702, 38.6521203), (-18.1239696, 179.0122737), (63.2467777, 25.9209164), (46.603354, 1.8883335), (-0.8999695, 11.6899699), (13.44294275, -16.118732162564726), (32.3293809, -83.1137366), (51.0834196, 10.4234469), (7.8573710000000005, -1.0840975468820433), (38.9953683, 21.9877132), (12.1360374, -61.6904045), (15.6356088, -89.8988087), (10.7226226, -10.7083587), (11.6688305, -15.535471371611191), (4.8417097, -58.6416891), (19.1399952, -72.3570972), (38.9247244, -77.06572732690151), (15.2572432, -86.0755145), (47.16115805, 19.135174733081314), (64.9841821, -18.1059013), (22.3511148, 78.6677428), (-2.4833826, 117.8902853), (32.6475314, 54.5643516), (33.0955793, 44.1749775), (52.865196, -7.9794599), (31.5313113, 34.8667654), (42.6384261, 12.674297), (18.1850507, -77.3947693), (36.5748441, 139.2394179), (31.1667049, 36.941628), (47.2286086, 65.2093197), (-0.1667169, 37.48603689110992), (36.638392, 127.6961188), (42.5653218, 20.913641374540397), (29.2733964, 47.4979476), (41.5089324, 74.724091), (20.0171109, 103.378253), (56.8406494, 24.7537645), (33.8750629, 35.843409), (-29.6039267, 28.3350193), (5.7499721, -9.3658524), (26.4215275, 17.30331995251014), (47.1594125, 9.546911556940856), (55.3500003, 23.7499997), (49.8158683, 6.1296751), (52.4387696, 4.8185293), (-18.9249604, 46.4416422), (-13.2687204, 33.9301963), (4.5693754, 102.2656823), (4.7064352, 73.3287853), (16.3700359, -2.2900239), (35.9446731, 14.383630615858262), (8.9995549, 168.0002575), (20.2540382, -9.2399263), (-20.2759451, 57.5703566), (22.5000485, -100.0000375), (8.6065, 152.00846930625), (47.2879608, 28.5670941), (43.73844905, 7.424224092532953), (46.8250388, 103.8499736), (42.9868853, 19.5180992), (31.1728205, -7.3362482), (-19.302233, 34.9144977), (-23.2335499, 17.3231107), (28.1083929, 84.0917139), (52.5001698, 5.7480821), (-41.5000831, 172.8344077), (12.6090157, -85.2936911), (17.7356214, 9.3238432), (9.6000359, 7.9999721), (41.6171214, 21.7168387), (60.5000209, 9.0999715), (21.0000287, 57.0036901), (30.3308401, 71.247499), (8.559559, -81.1308434), (-5.6816069, 144.2489081), (-23.3165935, -58.1693445), (-6.8699697, -75.0458515), (12.7503486, 122.7312101), (52.215933, 19.134422), (39.4332242, -8.567392135367742), (25.3336984, 51.2295295), (45.9852129, 24.6859225), (64.6863136, 97.7453061), (-1.9646631, 30.0644358), (17.250512, -62.6725973), (13.8250489, -60.975036), (12.90447, -61.2765569), (-13.7693895, -172.1200508), (43.9458623, 12.458306), (0.8875498, 6.9648718), (25.6242618, 42.3528328), (14.46517725, -14.765340959100413), (44.1534121, 20.55144), (-4.6574977, 55.4540146), (8.6400349, -11.8400269), (1.357107, 103.8194992), (48.6726975, 19.63679421037233), (46.149034549999996, 14.626325753340627), (-8.7053941, 159.1070693851845), (8.3676771, 49.083416), (-28.579397200000002, 24.086794125366062), (7.8699431, 29.6667897), (39.3260685, -4.8379791), (7.5554942, 80.7137847), (14.5844444, 29.4917691), (4.1413025, -56.0771187), (59.6749712, 14.5208584), (46.813331250000005, 8.444947437939408), (34.6401861, 39.0494106), (23.9739374, 120.9820179), (38.6281733, 70.8156541), (-6.5247123, 35.7878438), (14.8971921, 100.83273), (-8.5151979, 125.8375756), (8.7800265, 1.0199765), (10.8677845, -60.9821067), (33.994952999999995, 9.367097748325554), (38.9597594, 34.9249653), (39.7837304, -100.4458825), (1.3769751000000001, 32.72391099113675), (49.4871968, 31.2718321), (24.0002488, 53.9994829), (54.7023545, -3.2765753), (-32.8755548, -56.0201525), (41.32373, 63.9528098), (-16.5255069, 168.1069154), (8.0018709, -66.1109318), (13.2904027, 108.4265113), (31.9049661, 35.2023413), (16.3471243, 47.8915271), (-14.5189121, 27.5589884), (-19.01688, 29.35365015971339)]
df2['geo_loc']=lat_lon
lat,lon=zip(*np.array(df2['geo_loc']))
df2['lat']=lat
df2['lon']=lon
df2
| Country | Confirmed | Recovered | Deaths | geo_loc | lat | lon | |
|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | 56226 | 49937 | 2467 | (33.7680065, 66.2385139) | 33.768006 | 66.238514 |
| 1 | Albania | 122767 | 87760 | 2184 | (41.000028, 19.9999619) | 41.000028 | 19.999962 |
| 2 | Algeria | 116543 | 81065 | 3071 | (28.0000272, 2.9999825) | 28.000027 | 2.999983 |
| 3 | Andorra | 11687 | 11111 | 114 | (42.5407167, 1.5732033) | 42.540717 | 1.573203 |
| 4 | Angola | 21914 | 20190 | 532 | (-11.8775768, 17.5691241) | -11.877577 | 17.569124 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 187 | Vietnam | 2579 | 2265 | 35 | (13.2904027, 108.4265113) | 13.290403 | 108.426511 |
| 188 | West Bank and Gaza | 232038 | 205289 | 2521 | (31.9049661, 35.2023413) | 31.904966 | 35.202341 |
| 189 | Yemen | 3816 | 1580 | 810 | (16.3471243, 47.8915271) | 16.347124 | 47.891527 |
| 190 | Zambia | 87318 | 83895 | 1191 | (-14.5189121, 27.5589884) | -14.518912 | 27.558988 |
| 191 | Zimbabwe | 36778 | 34555 | 1518 | (-19.01688, 29.35365015971339) | -19.016880 | 29.353650 |
192 rows × 7 columns
df2.drop(['geo_loc'],axis=1,inplace=True)
df2
| Country | Confirmed | Recovered | Deaths | lat | lon | |
|---|---|---|---|---|---|---|
| 0 | Afghanistan | 56226 | 49937 | 2467 | 33.768006 | 66.238514 |
| 1 | Albania | 122767 | 87760 | 2184 | 41.000028 | 19.999962 |
| 2 | Algeria | 116543 | 81065 | 3071 | 28.000027 | 2.999983 |
| 3 | Andorra | 11687 | 11111 | 114 | 42.540717 | 1.573203 |
| 4 | Angola | 21914 | 20190 | 532 | -11.877577 | 17.569124 |
| ... | ... | ... | ... | ... | ... | ... |
| 187 | Vietnam | 2579 | 2265 | 35 | 13.290403 | 108.426511 |
| 188 | West Bank and Gaza | 232038 | 205289 | 2521 | 31.904966 | 35.202341 |
| 189 | Yemen | 3816 | 1580 | 810 | 16.347124 | 47.891527 |
| 190 | Zambia | 87318 | 83895 | 1191 | -14.518912 | 27.558988 |
| 191 | Zimbabwe | 36778 | 34555 | 1518 | -19.016880 | 29.353650 |
192 rows × 6 columns
import folium
folium.Map(zoom_start=2)
# Create a map
m = folium.Map(location=[54, 15], tiles='openstreetmap',zoom_start=2)
# Add points to the map
for id,row in df2.iterrows():
folium.Marker(location=[row['lat'],row['lon']], popup=row['Confirmed']).add_to(m)
# Display the map
m
for id,row in df2.iterrows():
folium.Marker(location=[row['lat'],row['lon']], popup=row['Recovered']).add_to(m)
# Display the map
m
m = folium.Map(location=[54, 15], tiles='openstreetmap', zoom_start=2)
# Add points to the map
for idx, row in df2.iterrows():
folium.Marker([row['lat'], row['lon']], popup=row['Deaths']).add_to(m)
# Display the map
m
m = folium.Map(location=[54,15], tiles='cartodbpositron', zoom_start=2)
# Add points to the map
from folium.plugins import MarkerCluster
mc = MarkerCluster()
for idx, row in df2.iterrows():
mc.add_child(folium.Marker([row['lat'], row['lon']],popup=row['Confirmed']))
m.add_child(mc)
# Display the map
m
from folium.plugins import HeatMap
df2
| Country | Confirmed | Recovered | Deaths | lat | lon | |
|---|---|---|---|---|---|---|
| 0 | Afghanistan | 56226 | 49937 | 2467 | 33.768006 | 66.238514 |
| 1 | Albania | 122767 | 87760 | 2184 | 41.000028 | 19.999962 |
| 2 | Algeria | 116543 | 81065 | 3071 | 28.000027 | 2.999983 |
| 3 | Andorra | 11687 | 11111 | 114 | 42.540717 | 1.573203 |
| 4 | Angola | 21914 | 20190 | 532 | -11.877577 | 17.569124 |
| ... | ... | ... | ... | ... | ... | ... |
| 187 | Vietnam | 2579 | 2265 | 35 | 13.290403 | 108.426511 |
| 188 | West Bank and Gaza | 232038 | 205289 | 2521 | 31.904966 | 35.202341 |
| 189 | Yemen | 3816 | 1580 | 810 | 16.347124 | 47.891527 |
| 190 | Zambia | 87318 | 83895 | 1191 | -14.518912 | 27.558988 |
| 191 | Zimbabwe | 36778 | 34555 | 1518 | -19.016880 | 29.353650 |
192 rows × 6 columns
# Create map with overall cases registered
m = folium.Map(location=[54,15], zoom_start=2)
HeatMap(data=df2[['lat', 'lon','Confirmed']], radius=15).add_to(m)
# Show the map
m